package com.example.dataextraction.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.dataextraction.model.QueryVO;
import com.example.dataextraction.model.vo.CopyTableVO;
import com.example.dataextraction.service.FetchDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api(tags = "抽数控制器")
@RestController
@RequestMapping("/fetchData")
public class FetchDataController {

    private static Logger logger = LoggerFactory.getLogger(FetchDataController.class);


    @Autowired
    private FetchDataService fetchDataService;

    @ApiOperation(value = "创建数据库")
    @PostMapping("/createDataBase")
    public Object createDataBase(@RequestParam(defaultValue = "DORIS") String dataSourceCode, @RequestParam String dataBaseName) {
        try {
            fetchDataService.createDataBase(dataSourceCode, dataBaseName);
            return "数据库创建成功！";
        } catch (Exception e) {
            logger.error("------------->/fetchData/createDataBase 接口异常: ", e);
            return "数据库创建失败：" + e.getMessage();
        }
    }

    @ApiOperation(value = "复制doris表")
    @PostMapping("/copyTable")
    public Object copyTable(@Validated @RequestBody CopyTableVO copyTableVO) {
        try {
            fetchDataService.copyTable(copyTableVO);
            return copyTableVO.getToDataBaseName() + "." + copyTableVO.getFromTableName() + "复制成功！";
        } catch (Exception e) {
            logger.error("------------->/fetchData/createTable 接口异常: ", e);
            return "数据库创建失败：" + e.getMessage();
        }
    }

    @ApiOperation(value = "抽数")
    @PostMapping("/copyData")
    public Object copyData(@Validated @RequestBody CopyTableVO copyTableVO) {
        try {
            fetchDataService.copyData(copyTableVO);
            return copyTableVO.getToDataBaseName() + "." + copyTableVO.getFromTableName() + "抽数成功！";
        } catch (Exception e) {
            logger.error("------------->/fetchData/createTable 接口异常: ", e);
            return "数据库创建失败：" + e.getMessage();
        }
    }

    @ApiOperation(value = "执行sql")
    @PostMapping("/executeSQL")
    public Object executeSQL(@RequestBody QueryVO queryVO) {
        try {
            if (true == queryVO.getLimit()) {
                queryVO.setPageNum(queryVO.getPageNum() == null ? 1 : queryVO.getPageNum());
                queryVO.setPageSize(queryVO.getPageSize() == null ? 10 : queryVO.getPageSize());
            }
            Object result = fetchDataService.executeSQL(queryVO);
            return result;
        } catch (Exception e) {
            logger.error("------------->/fetchData/executeSQL 接口异常: ", e);
            return "执行sql失败：" + e.getMessage();
        }
    }

    @ApiOperation(value = "检测Doris状态")
    @PostMapping("/checkDorisStatus")
    public Object checkDorisStatus(String dataSourceCode) {
        try {
            fetchDataService.checkDorisStatus(dataSourceCode);
            return "doris集群状态正常";
        } catch (Exception e) {
            logger.error("------------->/fetchData/checkDorisStatus 接口异常: ", e);
            return "执行sql失败：" + e.getMessage();
        }
    }

    @ApiOperation(value = "造数")
    @PostMapping("/createData")
    public Object createData(String dataSouceCode) {
        try {
            fetchDataService.createData(dataSouceCode);
            return "";
        } catch (Exception e) {
            logger.error("------------->/fetchData/checkDorisStatus 接口异常: ", e);
            return "执行sql失败：" + e.getMessage();
        }
    }

    @ApiOperation(value = "获取列元数据信息")
    @PostMapping("/getColumnInfo")
    public Object getColumnInfo(@RequestParam(defaultValue = "DORIS") String dataSourceCode, @RequestParam String databaseName, @RequestParam String tableName) {
        try {
            List<JSONObject> result = fetchDataService.getColumnInfo(dataSourceCode, databaseName, tableName);
            return result;
        } catch (Exception e) {
            logger.error("------------->/fetchData/checkDorisStatus 接口异常: ", e);
            return "执行sql失败：" + e.getMessage();
        }
    }
}